package com.yonyou.iuap.shell.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.yonyou.iuap.pub.DateUtil;
import com.yonyou.iuap.pub.IDFactory;
import com.yonyou.iuap.shell.dao.BdCompatibleEntityDao;
import com.yonyou.iuap.shell.dao.BdCompatibleRelationEntityDao;
import com.yonyou.iuap.shell.entity.BdCompatibleEntity;
import com.yonyou.iuap.shell.entity.BdCompatibleRelationEntity;
import com.yonyou.iuap.shell.entity.exp.CompatiblePojo;
import com.yonyou.iuap.shell.entity.exp.ShellCompatiblePoJo;
import com.yonyou.iuap.shell.pub.IShellDaoEnum;
import com.yonyou.iuap.shell.pub.IShellServiceEnum;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.ICompatibleService;
/**
 * 壳牌产品维护
 * **/
@Service(IShellServiceEnum.Shell_Service_Compatible)
public class CompatibleServiceImpl implements ICompatibleService{

	@Autowired
	@Qualifier(IShellDaoEnum.Shell_Dao_Compatible)
	private BdCompatibleEntityDao<BdCompatibleEntity> bdCompatibleEntityDao;
	@Autowired
	@Qualifier(IShellDaoEnum.Shell_Dao_Compatiblerealtion)
	private BdCompatibleRelationEntityDao comRelationDao;
	
	public ResultDto deleteByPrimaryKey(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
		}else{
			int i = bdCompatibleEntityDao.deleteByPrimaryKey(paramString);
			//删除关系表数据
			comRelationDao.deleteByCondition(" pk_compatible='"+paramString+"'");
			rsDto.setStatus(0);
			rsDto.setMessage("删除成功!");
		}
		return rsDto;
	}
	@Transactional
	public ResultDto insert(BdCompatibleEntity paramT) {
		ResultDto rsDto = new ResultDto();
		String jp_str = paramT.getSdvar01();
		paramT.setSdvar01(null);
		paramT.setPk_compatible(IDFactory.getInstance().getID());
		paramT.setDr(0);
		paramT.setTs(DateUtil.getDateTime());
		paramT.setDtcreate(new Date());
		//查询产品名称是否存在
		int count = bdCompatibleEntityDao.countByCondition(" compatible_product_name ='"+paramT.getCompatible_product_name()+"'");
		if(count>0){
			rsDto.setStatus(1);
			rsDto.setMessage("该竞品名称已经存在!");
		}else{
			
			int i = bdCompatibleEntityDao.insertSelective(paramT);
			paramT.setSdvar01(jp_str);
			//System.out.println(paramT);
			if(i==0){
				rsDto.setStatus(1);
				rsDto.setMessage("新增失败!");
			}else{
				insertRelationData(paramT);
				rsDto.setStatus(0);
				rsDto.setMessage("新增成功!");
			}
		}
		
		return rsDto;
	}
	private void insertRelationData(BdCompatibleEntity vo ){
		//添加关系表数据
		if(!StringUtils.isEmpty(vo.getSdvar01())){
			String[] jps = vo.getSdvar01().split(",");
			for(String pk : jps){
				BdCompatibleRelationEntity relvo = new BdCompatibleRelationEntity();
				relvo.setPk_compatible_relation(IDFactory.getInstance().getID());
				relvo.setPk_compatible(vo.getPk_compatible());
				relvo.setPk_product(pk);
				relvo.setDr(0);
				relvo.setTs(DateUtil.getDateTime());
				relvo.setDtcreate(new Date());
				relvo.setPk_creator(vo.getPk_creator()==null?vo.getUpdate_user():vo.getPk_creator());
				comRelationDao.insertSelective(relvo);
			}
		}
	}
	public BdCompatibleEntity selectByPrimaryKey(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			BdCompatibleEntity entity = bdCompatibleEntityDao.selectByPrimaryKey(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(entity);
		}
		return null;
	}

	public ResultDto selectAll() {
		ResultDto rsDto = new ResultDto();
		List<BdCompatibleEntity> listvo = bdCompatibleEntityDao.queryAll();
		rsDto.setStatus(0);
		rsDto.setMessage("数据查询成功!");
		rsDto.setNum(listvo==null?0:listvo.size());
		rsDto.setCount(listvo==null?0:listvo.size());
		rsDto.setData(listvo);
		return rsDto;
	}

	public ResultDto updateByPrimaryKey(BdCompatibleEntity paramT) {
		ResultDto rsDto = new ResultDto();
		int i = bdCompatibleEntityDao.updateByPrimaryKey(paramT);
		if(i==0){
			rsDto.setStatus(1);
			rsDto.setMessage("修改失败!");
		}else{
			rsDto.setStatus(0);
			rsDto.setMessage("修改成功!");
		}
		return null;
	}

	public ResultDto selectByCondition(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(paramString==null||StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			List<BdCompatibleEntity>  listvo= bdCompatibleEntityDao.selectByCondition(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(listvo);
			rsDto.setNum(listvo==null?0:listvo.size());
		}
		return rsDto;
	}

	public ResultDto countByCondition(String paramString) {
		// TODO 自动生成的方法存根
		return null;
	}

	public ResultDto deleteByCondition(String paramString) {
		// TODO 自动生成的方法存根
		return null;
	}

	public ResultDto insertSelective(BdCompatibleEntity paramT) {
		// TODO 自动生成的方法存根
		return null;
	}
	@Transactional
	public ResultDto updateByPrimaryKeySelective(BdCompatibleEntity paramT) {
		ResultDto rsDto = new ResultDto();
		String product_str = paramT.getSdvar01();
		paramT.setSdvar01(null);
		int i = bdCompatibleEntityDao.updateByPrimaryKeySelective(paramT);
		if(i==0){
			rsDto.setStatus(1);
			rsDto.setMessage("修改失败!");
		}else{
			//判断修改的名称是否已经存在
			int count = bdCompatibleEntityDao.countByCondition(" compatible_product_name ='"+paramT.getCompatible_product_name()+"' and pk_compatible!='"+paramT.getPk_compatible()+"'");
			if(count>0){
				rsDto.setStatus(1);
				rsDto.setMessage("该名称已经存在！");
			}else{
				//首先删除关系表数据在进行添加操作
				String delWher = " pk_compatible='"+(paramT.getPk_compatible()==null?"null":paramT.getPk_compatible())+"'";
				comRelationDao.deleteByCondition(delWher);
				paramT.setSdvar01(product_str);
				insertRelationData(paramT);
				rsDto.setStatus(0);
				rsDto.setMessage("修改成功!");
			}
			
		}
		return rsDto;
	}

	public ResultDto queryCompatible(Map param) {
		ResultDto rsDto = new ResultDto();
		if(param==null||StringUtils.isEmpty(param.get("pk_product"))){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			List<CompatiblePojo>  listvo= bdCompatibleEntityDao.queryCompatible(param);
			int count=bdCompatibleEntityDao.queryCompatiblecount(param);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(listvo);
			rsDto.setCount(count);
			rsDto.setNum(listvo==null?0:listvo.size());
		}
		return rsDto;
	}

	public ResultDto pageQuery(RequestDto reqDto) {
		ResultDto rsDto = new ResultDto();
		List<ShellCompatiblePoJo> listvo = bdCompatibleEntityDao.pageQuery(reqDto);
		rsDto.setData(listvo);
		rsDto.setMessage("数据查询成功!");
		rsDto.setStatus(0);
		rsDto.setNum(listvo==null?0:listvo.size());
		//查询汇总数
		int count = bdCompatibleEntityDao.pageQueryCount(reqDto);
		rsDto.setCount(count);
		return rsDto;
	}
	/**
	 * 查询所有分页
	 * ***/
	public ResultDto selectAllPage(Map param) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		List<BdCompatibleEntity> listvo = bdCompatibleEntityDao.queryAllPage(param);
		int count=bdCompatibleEntityDao.queryAllPagecount(param);
		rsDto.setStatus(0);
		rsDto.setMessage("数据查询成功!");
		rsDto.setNum(listvo==null?0:listvo.size());
		rsDto.setCount(count);
		rsDto.setData(listvo);
		return rsDto;
	}
	
	public ResultDto selectByCondition(Map param) {
		// TODO 自动生成的方法存根
		return null;
	}
	
	public int selectByConditionPagecount(Map param) {
		// TODO 自动生成的方法存根
		return 0;
	}
	public int selectByConditionPage(String paramString) {
		// TODO 自动生成的方法存根
		return 0;
	}
	public ResultDto selectByConditionPage(Map param) {
		// TODO 自动生成的方法存根
		ResultDto rsDto = new ResultDto();
		List<BdCompatibleEntity> listvo = bdCompatibleEntityDao.selectByConditionPage(param);
		int count=bdCompatibleEntityDao.selectByConditionPagecount(param);
		rsDto.setStatus(0);
		rsDto.setMessage("数据查询成功!");
		rsDto.setNum(listvo==null?0:listvo.size());
		rsDto.setCount(count);
		rsDto.setData(listvo);
		return rsDto;
	}



	

	
}
